﻿@using Blazor.FlexGrid.Demo.Shared
@using Blazor.FlexGrid.DataAdapters
@using Blazor.FlexGrid.Components.Configuration
@inject HttpClient Http
@inject BlazorComponentColumnCollection<WeatherForecast> Collection
@page "/grid"

<h1>Weather forecast</h1>

<EditForm Model="@Model" OnValidSubmit="@HandleValidSubmit">
    <InputText id="name" @bind-Value="Model.Summary" />
    <GridView DataAdapter="@dataAdapter" PageSize="5"></GridView>
</EditForm>

@{
    RenderFragment<WeatherForecast> weatherTemp = (weather) => @<h4>@weather.Summary!!</h4>;
Func<EditColumnContext, RenderFragment<WeatherForecast>> editWeatherSummary = context =>
{
RenderFragment<WeatherForecast> editFragment = (weather) =>
@<input value=@weather.Summary @onchange=@((ChangeEventArgs e) => context.NotifyValueHasChanged(e.Value)) />;

return editFragment;
};


Collection.AddColumnValueRenderFunction(w => w.Summary, weatherTemp);
Collection.AddColumnEditValueRenderer(w => w.Summary, editWeatherSummary);
}

@code{
    WeatherForecast Model = new WeatherForecast();
    CollectionTableDataAdapter<WeatherForecast> dataAdapter;

    protected override async Task OnInitializedAsync()
    {
        var forecast = await Http.GetJsonAsync<WeatherForecast[]>("api/SampleData/WeatherForecastsSimple");
        dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
    }

    private void HandleValidSubmit()
    {

    }
}
